package com.xsm.ppsecret.util;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * Author: Xiasem
 * Email: xiasem@163.com
 * Date: 17-5-29.
 */

public class CryptUtil {

    /**
     * 使用两套密码的加密，强度更高
     * @param data 数据
     * @param key 第一个密码
     * @param ivData 第二个密码
     * @return byte[]
     */
    public static byte[] aesEncryptWithIv(byte[] data, byte[] key, byte[] ivData){
        byte[] ret = null;
        if (data != null && key != null && ivData != null) {
            if(data.length > 0 && key.length == 16 && ivData.length == 16){
                // 使用两套密码的，算法需要写成 AES/算法模式/填充模式
                try {
                    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                    // 准备第一套密码
                    SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
                    // 准备第二套密码
                    IvParameterSpec iv = new IvParameterSpec(ivData);
                    // 设置两套密码的初始化
                    cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
                    cipher.update(data);
                    ret = cipher.doFinal();
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidAlgorithmParameterException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }

    public static byte[] aesDecryptWithIv(byte[] data, byte[] key, byte[] ivData){
        byte[] ret = null;
        if (data != null && key != null && ivData != null) {
            if(data.length > 0 && key.length == 16 && ivData.length == 16){
                // 使用两套密码的，算法需要写成 AES/算法模式/填充模式
                try {
                    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                    // 准备第一套密码
                    SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
                    // 准备第二套密码
                    IvParameterSpec iv = new IvParameterSpec(ivData);
                    // 设置两套密码的初始化
                    cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
                    cipher.update(data);
                    ret = cipher.doFinal();
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidAlgorithmParameterException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }
}
